﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Data.GameData.GameMap;
using Data.GameData.Interfaces;
using WeifenLuo.WinFormsUI.Docking;

namespace GUIComponents
{
    public partial class GameMapWindow : DockContent, IGameWindow
    {
        private MapType mapType = MapType.STRATEGIC;
        private Guid mapOwner;

        #region Constructors
        /// <summary>
        /// Create a new Map Window with a new baord
        /// </summary>
        /// <param name="type">The type of map to create</param>
        public GameMapWindow(MapType type)
        {
            InitializeComponent();

            mapType = type;
            mapPanel.MapID = Map.CreateMap();
        }

        /// <summary>
        /// Create a new Map Window but use the information from another map
        /// </summary>
        /// <param name="type">The type of map to create</param>
        /// <param name="mapId">The existing map to display</param>
        public GameMapWindow(MapType type, int mapId)
        {
            InitializeComponent();

            mapType = type;
            mapPanel.MapID = mapId;
        }

        /// <summary>
        /// Overloaded to setup callback for Weifenluo fix.  Not used at this time as there seems to be no issue
        /// with this view (yet).
        /// </summary>
        /// <param name="e">The events for OnLoad</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.DockPanel != null)
            {
                // Not used at this time.
                //this.HandleDestroyed += new EventHandler(GameMapWindow_HandleDestroyed);
                //this.DockPanel.ActivePaneChanged += new EventHandler(GameMapWindow_ActivePaneChanged);
            }
        }
        #endregion

        #region Properties
        public MapType MapType
        {
            get { return mapType; }
            set { }
        }
        #endregion

        #region WeifenLuo fix callbacks
        /// <summary>
        /// Fix for Weifenluo to stop view contents being ´lost´ when tileing the views.
        /// </summary>
        /// <param name="sender">Not used</param>
        /// <param name="e">Not used</param>
        private void GameMapWindow_HandleDestroyed(object sender, EventArgs e)
        {
            mapPanel.Parent = null;
        }

        /// <summary>
        /// Fix for Weifenluo to stop view contents being ´lost´ when tileing the views.
        /// </summary>
        /// <param name="sender">Not used</param>
        /// <param name="e">Not used</param>
        private void GameMapWindow_ActivePaneChanged(object sender, EventArgs e)
        {
            if (IsHandleCreated)
            {
                mapPanel.Parent = FindForm();
                mapPanel.BringToFront();
            }
        }
        #endregion

        #region IGameWindow Members
        public new Guid Owner
        {
            get { return mapOwner; }
            set { mapOwner = value; }
        }

        public int Id
        {
            get { return mapPanel.MapID; }
            set { mapPanel.MapID = value; }
        }
        #endregion
    }
}
